package 面试v1.字节;

public class LeetCode543 {

    public int diameterOfBinaryTree1(TreeNode root) {
        if (root == null) {
            return 0;
        }

        return helper(root.left) + helper(root.right);
    }

    private int helper(TreeNode node) {
        if (node == null) {
            return 0;
        }

        return Math.max(helper(node.left), helper(node.right)) + 1;
    }

    int maxd = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return maxd;
    }

    public int depth(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int Left = depth(node.left);
        int Right = depth(node.right);
        maxd = Math.max(Left + Right, maxd);//将每个节点最大直径(左子树深度+右子树深度)当前最大值比较并取大者
        return Math.max(Left, Right) + 1;//返回节点深度
    }
}
